package com.dushuren.project.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dushuren.project.common.ErrorCode;
import com.dushuren.project.exception.BusinessException;
import com.dushuren.project.model.dto.SearchWordsRequest;
import com.dushuren.project.model.entity.SuggestNotWordsSearch;
import com.dushuren.project.model.entity.SuggestWordsSearch;
import com.dushuren.project.mapper.SuggestWordsSearchMapper;
import com.dushuren.project.model.enums.SuggestSearchSelectEnum;
import com.dushuren.project.model.vo.SuggestWordsSearchVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.dushuren.project.service.SuggestWordsSearchService;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author qieqie
* @description 针对表【suggest_words_search(推荐收搜索)】的数据库操作Service实现
* @createDate 2022-11-02 09:10:03
*/
@Service
public class SuggestWordsSearchServiceImpl extends ServiceImpl<SuggestWordsSearchMapper, SuggestWordsSearch>
    implements SuggestWordsSearchService{

  @Override
  public Page<SuggestWordsSearchVo> queryListData(SearchWordsRequest searchWordsRequest) {
	String searchInformation = searchWordsRequest.getSearchInformation();
	long current = searchWordsRequest.getCurrent();
	long pageSize = searchWordsRequest.getPageSize();
	QueryWrapper<SuggestWordsSearch> queryWrapper = new QueryWrapper<>();
	queryWrapper.select("id", "searchInformation", "searchFirst", "searchNumber", "createTime");
	queryWrapper.orderByDesc("searchFirst");
	queryWrapper.like(StringUtils.isNotBlank(searchInformation), "searchInformation", searchInformation);
	Page<SuggestWordsSearch> pageResult = this.page(new Page<>(current, pageSize), queryWrapper);
	List<SuggestWordsSearchVo> suggestWordsSearchVo = this.getSuggestWordsSearchVo(pageResult);

	Page<SuggestWordsSearchVo> page = new Page<>(pageResult.getSize(), pageResult.getCurrent(), pageResult.getTotal());
	page.setRecords(suggestWordsSearchVo);

	return page;
  }

  public List<SuggestWordsSearchVo> getSuggestWordsSearchVo(Page<SuggestWordsSearch> page) {
	// todo 将其合并成一个 方法
	return page.getRecords().stream().map(suggestWordsSearch -> {
	  SuggestWordsSearchVo suggestWordsSearchVo = new SuggestWordsSearchVo();
	  suggestWordsSearchVo.setId(suggestWordsSearch.getId());
	  suggestWordsSearchVo.setSearchCategory(0L);
	  suggestWordsSearchVo.setSearchFirst(suggestWordsSearch.getSearchFirst());
	  suggestWordsSearchVo.setSearchInformation(suggestWordsSearch.getSearchInformation());
	  suggestWordsSearchVo.setSearchNumber(suggestWordsSearch.getSearchNumber());
	  suggestWordsSearchVo.setCreateTime(suggestWordsSearch.getCreateTime());
	  return suggestWordsSearchVo;
	}).collect(Collectors.toList());
  }

  @Override
  public Long deleteSearchWords(Long searchId) {
	try {
	  this.removeById(searchId);
	} catch (Exception e) {
	  throw new BusinessException(ErrorCode.SYSTEM_ERROR, "传入值异常");
	}
	return 0L;
  }

  @Override
  public Long updateSearchWords(SuggestWordsSearch suggestWordsSearch) {
	try {
	  this.updateById(suggestWordsSearch);
	} catch (Exception e) {
	  throw new BusinessException(ErrorCode.SYSTEM_ERROR, "传入值异常");
	}
	return 0L;
  }

  @Override
  public Long addSearchWords(SuggestWordsSearch suggestWordsSearch) {
	try {
	  this.save(suggestWordsSearch);
	} catch (Exception e) {
	  throw new BusinessException(ErrorCode.SYSTEM_ERROR, "传入值异常");
	}
	return null;
  }
}




